<template>
  <div class="employee-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <div class="search-label">员工姓名：</div>
      <el-input
        clearable
        placeholder="请输入内容"
        v-model="form.name"
        class="search-main"
      />
      <el-button type="primary" @click="chaxun">查询</el-button>
    </div>
    <div class="create-container">
      <el-button type="primary" @click="addyg()">添加员工</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table style="width: 100%" :data="employeeList">
        <el-table-column type="index" label="序号" />
        <el-table-column label="员工姓名" width="180" prop="name" />
        <el-table-column label="登录账号" width="180" prop="userName" />
        <el-table-column label="联系方式" width="120" prop="phonenumber" />
        <el-table-column label="角色" width="120" prop="roleName" />
        <el-table-column label="状态">
          <template #default="scope">
            <span v-if="scope.row.status == 0">未启用</span>
            <span v-if="scope.row.status == 1">启用</span>
          </template>
        </el-table-column>
        <el-table-column label="添加时间" prop="createTime" />
        <el-table-column label="操作" fixed="right" width="120">
          <template #default="scope">
            <el-button size="mini" type="text" @click="edit(scope.row)"
              >编辑</el-button
            >
            <el-button size="mini" type="text" @click="shanchu(scope.row.id)">删除</el-button>
            <el-button size="mini" type="text" @click="czmm(scope.row.id)">重置密码</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page-container">
      <el-pagination
        layout="total, prev, pager, next"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="form.page"
        :page-size="form.pageSize"
      />
    </div>
    <!-- 添加员工 -->
    <el-dialog
      :title="title"
      :visible="dialogVisible"
      width="580px"
      @close="closeDialog"
    >
      <!-- 表单接口 -->
      <div class="form-container">
        <el-form
          ref="addForm"
          :model="addForm"
          :rules="addFormRules"
          label-width="80px"
        >
          <el-form-item label="员工姓名" prop="name">
            <el-input v-model="addForm.name" />
          </el-form-item>
          <el-form-item label="登录账号" prop="userName">
            <el-input v-model="addForm.userName" />
          </el-form-item>
          <el-form-item label="联系方式" prop="phonenumber">
            <el-input v-model="addForm.phonenumber" />
          </el-form-item>
          <el-form-item label="分配角色" prop="roleId">
            <el-select v-model="addForm.roleId" placeholder="请选择角色">
              <el-option
                v-for="item in roleList"
                :key="item.roleId"
                :label="item.roleName"
                :value="item.roleId"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="员工状态" prop="status">
            <el-radio-group v-model="addForm.status">
              <el-radio :label="0">禁用</el-radio>
              <el-radio :label="1">启用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <el-button size="mini" @click="dialogVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="submit()"
          >确 定</el-button
        >
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { addyuangong, edityuangong, getyuangong, rolelistxl, userchongzhi, userdelete } from "@/api/user";

export default {
  name: "Employee",
  data() {
    return {
      employeeList: [],
      tabledata: [],
      form: {
        page: 1,
        pageSize: 10,
      },
      title: "添加员工",
      total: 0,
      roleList: [],
      addFormRules: {
        name: [{ required: true, message: "请输入员工姓名", trigger: "blur" }],
        userName: [
          { required: true, message: "请输入登录账号", trigger: "blur" },
        ],
        phonenumber: [
          { required: true, message: "请输入联系方式", trigger: "blur" },
        ],
        roleId: [
          { required: true, message: "请选择分配角色", trigger: "change" },
        ],
        status: [
          { required: true, message: "请选择员工状态", trigger: "change" },
        ],
      },
      addForm: {},
      dialogVisible: false,
    };
  },
  mounted() {
    this.getdata();
    this.getrole();
  },
  methods: {
    czmm(id){
   this.$confirm("是否将密码重置为123456?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const res = await userchongzhi(id);
          if (res.code == 10000) {
            this.$message({
              type: "success",
              message: "重置成功!",
            });
            this.getdata();
          } else {
            this.$message({
              type: "info",
              message: res.msg,
            });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消",
          });
        });
    },
      shanchu(id) {
      this.$confirm("是否确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const res = await userdelete(id);
          if (res.code == 10000) {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.getdata();
          } else {
            this.$message({
              type: "info",
              message: res.msg,
            });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    edit(row) {
      this.addForm = JSON.parse(JSON.stringify(row));
      this.dialogVisible = true;
      this.title = "编辑员工";
    },
    closeDialog() {
      this.dialogVisible = false;
    },
    async getrole() {
      const res = await rolelistxl();
      this.roleList = res.data;
    },
    async submit() {
      this.$refs.addForm.validate(async (valid) => {
        if (valid) {
          if (this.title == "添加员工") {
            const res = await addyuangong(this.addForm);
            console.log(res, "res");
            if (res.code == 10000) {
              this.$message.success("成功");
              this.getdata();
              this.dialogVisible = false;
            }
          } else {
            delete this.addForm.createTime
            const res = await edityuangong(this.addForm);
            console.log(res, "res");
            if (res.code == 10000) {
              this.$message.success("成功");
              this.getdata();
              this.dialogVisible = false;
            }
          }
        }
      });
    },
    addyg() {
      this.dialogVisible = true;
      this.title = "添加员工";
      this.addForm = {};
    },
    handleSizeChange(e) {
      this.form.pageSize = e;
      this.getdata();
    },
    handleCurrentChange(e) {
      this.form.page = e;
      this.getdata();
    },
    chaxun() {
      this.form.page = 1;
      this.form.pageSize = 10;
      this.getdata();
    },
    async getdata() {
      const res = await getyuangong(this.form);
      console.log(res, "res");
      this.employeeList = res.data.rows;
    },
  },
};
</script>

<style lang="scss" scoped>
.employee-container {
  padding: 10px;
  background: #fff;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, 0.9);
  padding-bottom: 20px;

  .search-label {
    width: 100px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}
.create-container {
  margin: 10px 0px;
}
.page-container {
  padding: 4px 0px;
  text-align: right;
}
.form-container {
  padding: 0px 80px;
}
</style>